<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/1 0001
 * Time: 10:36
 */

if(!function_exists('guid'))
{
    function guid() {
        if (function_exists('com_create_guid')) {
            return com_create_guid();
        } else {
            mt_srand((double)microtime()*10000);
            $charid = strtoupper(md5(uniqid(rand(), true)));
            $hyphen = '';//chr(45);
            $uuid   = '' //chr(123)
                .substr($charid, 0, 8).$hyphen
                .substr($charid, 8, 4).$hyphen
                .substr($charid,12, 4).$hyphen
                .substr($charid,16, 4).$hyphen
                .substr($charid,20,12)
            ;//.chr(125);
            return $uuid;
        }
    }
}

if(!function_exists('getSalt'))
{
    function getSalt($len=6){
        $saltchars = '1234567890qwertyuiopasdfghjklzxcvbnm';
        srand((double)microtime()*1000000);
        $salt = '';
        for($i=0;$i<$len;$i++){
            $salt = $salt.substr($saltchars,rand()%strlen($saltchars),1);
        }
        return $salt;
    }
}


if(!function_exists('removeDir')){
    function removeDir($dirName)
    {
        if(! is_dir($dirName))
        {
            return false;
        }
        $handle = @opendir($dirName);
        while(($file = @readdir($handle)) !== false)
        {
            if($file != '.' && $file != '..')
            {
                $dir = $dirName . '/' . $file;
                is_dir($dir) ? removeDir($dir) : @unlink($dir);
            }
        }
        closedir($handle);

        return rmdir($dirName) ;
    }
}

if(!function_exists('copyDir')){
    function copyDir($src,$dst){
        $dir = opendir($src);
        @mkdir($dst);
        while(false !== ($file=readdir($dir))){
            if(($file != '.') && ($file != '..')){
                if(is_dir($src.'/'.$file)){
                    copyDir($src.'/'.$file,$dst.'/'.$file);
                }
                else{
                    copy($src.'/'.$file,$dst.'/'.$file);
                }
            }
        }
        closedir($dir);
    }
}

if(!function_exists('getDirFiles')){
    function getDirFiles($dir,$scansubdir=false,$deepth=10){
        $res = array();
        if($deepth<=0)
            return array();
        $list = scandir($dir);
        foreach($list as $file){
            $filelocation = $dir.'/'.$file;
            if(in_array($file,array('.','..')))
                continue;
            if(is_dir($filelocation) && $scansubdir)
            {
                $subfiles = getDirFiles($filelocation,$scansubdir,$deepth-1);
                $res = array_merge($res,$subfiles);
            }
            else{
                $res[] = $filelocation;
            }
        }
        return $res;
    }
}
